<?php

namespace App\Http\Controllers\Home;

use App\Models\Home\Property;
use App\Services\Home\AttributeService;
use App\Services\Home\PropertyService;
use App\Services\Home\RegionService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PropertyController extends Controller
{
    protected $service;
    protected $attrService;
    protected $regionService;

    public function __construct(PropertyService $service, AttributeService $attributeService,RegionService $regionService)
    {
        $this->service = $service;
        $this->attrService = $attributeService;
        $this->regionService = $regionService;
    }

    public function index(Request $request,$id)
    {
        // 市id
        $areaid = isset($id)?$id:0;
        $city_info = $this->regionService->getCityInfo($areaid);
        // 特色id
        $tid = $request->get('t');
//        $uni = $this->service->getUniqueid($tid);
        $featureid = isset($tid)?$tid:0;
        //属性
        $area = $this->attrService->getAttributeById($areaid);
        // 价格区间
        $price = $this->attrService->getAttributeById(6);
        // 户型
        $apartment = $this->attrService->getAttributeById(18);
        // 特色
        $feature = $this->attrService->getAttributeById(34);
        // 物业
        $wytype = $this->attrService->getAttributeById(42);
        // 装修情况
        $renovation = $this->attrService->getAttributeById(47);

        //搜索词
        $keyword = $request->get('keyword')??'';

        return view(env('DEFAULT_TEMPLATE').'.property',compact('area','areaid','featureid','price','apartment','wytype','feature','renovation','city_info','keyword'));
    }

    public function data(Request $request) {
        return $this->service->data($request);
    }

    public function getMapPosition(Request $request)
    {
        $pos = explode(',',$request->get('pos'));
        $title = $request->get('n');
        $lng = $pos[0];
        $lat = $pos[1];
        return view(env('DEFAULT_TEMPLATE').'.mapos',compact('title','lng','lat'));
    }

    public function show($id)
    {
        $data = $this->service->getDetailById($id);
        //户型
        $apartment = $this->service->getPropertyApartment($id);
        //相册
        $albums = $this->service->getPropertyAlbum($id);
        $first = array_key_first($albums->toArray());
        return view(env('DEFAULT_TEMPLATE').'.detail',compact('data','apartment','albums','first'));
    }

}
